#include "config.h"


/**
 * @brief Fast::config::config
 * @param fn
 * @details 从文件中加载
 */
Fast::config::config(const std::string &fn)
{
    path = fn;
    std::string ext =SplitFilename(fn);
    ext_format = GetIndex(ext);
    import(fn);
    isFile = true;
}

/**
 * @brief Fast::config::putStr
 * @param str
 * @param ext
 * @details 从数据流中获取数据
 */
Fast::config::config(std::string str,std::string ext)
{
    ext_format = GetIndex(ext);
    std::stringstream str_stream(str);
    import(str);
    isFile = false;
}


Fast::config::config(std::string_view str, const char *ext)
{
    std::string extf = ext;
    ext_format = GetIndex(extf);
    std::string ff(str.begin(),str.end());
    std::stringstream str_stream(ff);
    import(str_stream);
    isFile = false;
}

void Fast::config::sync()
{
    this->write2File();
}



void Fast::config::write2File()
{
    if(!isFile){
        std::cout <<"当前不是文件，无法进行保存操作!" <<std::endl;
        return;
    }
    std::ostringstream outputStream;
    outputStream = write2OS();

    std::ofstream tmpReport(path);
    if(!tmpReport){
        std::cout <<"文件不能打开:" << path <<std::endl;
    }else{
        tmpReport<<outputStream.str();
    }
    tmpReport.close();
}

/**
 * @brief Fast::config::write2File
 * @param m_path
 * @details 写入到指定的文件中
 */
void Fast::config::write2File(std::string m_path)
{
    std::ostringstream outputStream;
    outputStream = write2OS();
    std::ofstream tmpReport(m_path);
    if(!tmpReport){
        std::cout <<"文件不能打开:" << m_path <<std::endl;
    }else{
        tmpReport<<outputStream.str();
    }
    tmpReport.close();
}

/**
 * @brief Fast::config::import
 * @param data
 * @details 将文件或者数据流写入到boost对象
 */
void Fast::config::import(std::string data)
{
    switch (ext_format) {
    case T_JSON:
        boost::property_tree::read_json(data,pt);
        break;
    case T_INFO:
        boost::property_tree::read_info(data,pt);
        break;
    case T_XML:
        boost::property_tree::read_xml(data,pt);
        break;
    case T_INI:
        boost::property_tree::read_ini(data,pt);
        break;
    default:
        std::cout <<"-->" << "Error format: "<< data <<std::endl;
        break;
    }
}

/**
 * @brief Fast::config::import
 * @param data
 * @details 将文件或者数据流写入到boost对象
 */
void Fast::config::import(std::stringstream &data)
{
    switch (ext_format) {
    case T_JSON:
        boost::property_tree::read_json(data,pt);
        break;
    case T_INFO:
        boost::property_tree::read_info(data,pt);
        break;
    case T_XML:
        boost::property_tree::read_xml(data,pt);
        break;
    case T_INI:
        boost::property_tree::read_ini(data,pt);
        break;
    default:
        std::cout <<"-->" << "Error format!"<<std::endl;
        break;
    }
}

// 将boost对象写入到文件流
std::ostringstream Fast::config::write2OS()
{
    std::ostringstream outputStream;
    switch (ext_format) {
    case T_INI:
        boost::property_tree::ini_parser::write_ini(outputStream, pt,false);
        break;
    case T_INFO:
        boost::property_tree::info_parser::write_info(outputStream, pt);
        break;
    case T_JSON:
         boost::property_tree::json_parser::write_json(outputStream, pt,false);
        break;
    case T_XML:
        boost::property_tree::xml_parser::write_xml(outputStream, pt,false);
        break;
    default:
        std::cout <<"Error format:" << ext_format <<std::endl;
        return outputStream;
    }
    return outputStream;
}

